<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    public function index()
    {
        try {
            //$menu_id = input('menu_id','-1','htmlspecialchars');//第三个参数过滤
            $language = input("lang", "cn", "htmlspecialchars");
            if ($language == "cn") {
                $content = "content_cn";
                $brand_name = "brand_name_cn";
                $img_url = "img_url_cn";

            } elseif ($language == "en") {
                $content = "content_en";
                $brand_name = "brand_name_en";
                $img_url = "img_url_en";
            }

            //查询index表，index-carousel 6是轮播图 7是标签
            $carousel = Db::query("select index_id,type_id,$content as content from ls_index WHERE delete_time is null ");

            //查询brand表，brand-brand 6是轮播图 7是标签
            $brand = Db::query("select brand_id,type_id,$brand_name as brand_name,$img_url as img_url,href from ls_brand WHERE delete_time is null");

            return json([
                'code' => 200,
                'carousel' => $carousel,
                'brand' => $brand,
                "imsg" => "首页-index.html"
            ]);

        }catch (\Exception $e){
            // $this->_log2db('index::被 catch 了');
            return json([
                'code'=>2333,
                'imsg'=>'被catch',
                'message'=>'数据库查询失败 '.$e->getMessage(),
            ]);
        }

    }
    //return "无权访问";

    public function imageSrc() {
        try {
            $content = $this->request->param('condition');
            \think\Db::table('tb_test')->insert([
                'title' => $content
            ]);
        } catch (\Exception $e) {
            \think\Db::table('tb_test')->insert([
                'title' => $e->getMessage()
            ]);
        }
    }

    public function dbTest() {
        try {
            $content = $this->request->post('content');


            // \think\Db::table('tb_test')->insert([
            //     'big_content' => $content
            // ]);


            $str = substr($content, 0, 50);
            $commaPosition = strpos($str, ',');
            $commaPosition++;
            $mime = substr($str, 5, (strpos($str, ';') - 5));
            $imageType = substr($mime, 6);
            file_put_contents("G:\\study\\study-php-thinkphp\\tp505\\public\\static\\" . time() . '.jpg', base64_decode(substr($content, $commaPosition)));


            return json([
                'errcode' => 200,
                'message' => substr($content, 0, 40)
            ]);
        } catch (\Exception $e) {
            return json([
                'errcode' => 4000,
                'message' => $e->getMessage()
            ]);
        }
    }
}